package cn.bdqn.controller.admin;

import cn.bdqn.dao.User.UserMapper;
import cn.bdqn.dto.LoginUser;
import cn.bdqn.pojo.User.User;
import cn.bdqn.service.Role.RoleService;
import cn.bdqn.service.user.UserService;
import cn.bdqn.utils.Json;
import cn.bdqn.utils.Page;
import cn.bdqn.utils.RegexUtils;
import cn.bdqn.utils.SensitiveWordUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SensitiveWordUtil sensitiveWordUtil;

    /**
     * 查询所有管理员
     * @param page
     * @return 管理员信息
     */
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/getAllAdmin")
    public Json getAllAdmin(@RequestBody Page page){
        return userService.getAllAdmin(page.getPageNum(),page.getPageSize(), page.getName(),page.getPhone(),page.getRoleId());
    }

    /**
     * 查询管理员的角色信息
     * @return
     */
    @PreAuthorize("hasAuthority('admin')")
    @GetMapping("/getAllAdminRole")
    public Json getAlAdminRole(){
        return roleService.getAllAdmiRole();
    }

    /**
     * 修改管理员的状态
     * @param userid 管理员的编号
     * @param state  状态编号
     * @return
     */
    @PreAuthorize("hasAuthority('admin')")
    @DeleteMapping("/{userid}/{state}")
    public Json deleteAdmin(@PathVariable Integer userid, @PathVariable Integer state){
        return userService.deleteAdmin(userid,state);
    }

    /**
     * 改变管理员的角色
     * @param userid 管理员编号
     * @param roleid 角色编号
     * @return
     */
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/changeRole/{userid}/{roleid}")
    public Json updateRoleByAdmin(@PathVariable Integer userid,@PathVariable Integer roleid ){
        return roleService.updateRoleByAdmin(userid,roleid);
    }

    /**
     * 添加管理员
     * @param user
     * @return
     */
    @PreAuthorize("hasAuthority('admin')")
    @PostMapping("/register")
    public Json registerAdmin(@RequestBody User user){
        log.info("suer ===>" +user+"-----------");
        Boolean sensitiveWord = sensitiveWordUtil.isSensitiveWord(user.getUserName());
        if (sensitiveWord == false){
            return Json.fall("您的名称包含敏感词,请重新设置");
        }
        if (!RegexUtils.isPhoneInvalid(user.getPhone())) {
            return Json.fall("您的手机号可能有误,请检查后再试,谢谢");
        }
        if (!RegexUtils.isEmailInvalid(user.getEmail())) {
            return Json.fall("您的邮箱可能有误,请检查后再试,谢谢");
        }
        User userByPhone = userMapper.getUserByPhone(user.getPhone());
        if (userByPhone != null){
            return Json.fall("该用户以存在");
        }
        return userService.inserAdmin(user);
    }

}
